<?php

namespace App\Models;

use App\Models\Business\ServiceCategory;
use App\Models\SubAccount\SubAccount;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Sanctum\HasApiTokens;

class CustomerManager extends Model
{
    use HasFactory,HasApiTokens;
    public function guardName(){
        return 'sanctum';
    }
    protected $appends=['shop_nums','sub_account','customer_complaints'];
    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format('Y/m/d H:i:s');
    }
    protected $guarded = [];
    public function person(){
        return $this->hasOne(SystemUser::class,'id','system_user_id');
    }
    public function first(){
        return $this->hasOne(Department::class,'id','department_one_id');
    }
    public function two(){
        return $this->hasOne(Department::class,'id','department_two_id');
    }
    public function three(){
        return $this->hasOne(Department::class,'id','department_three_id');
    }
    public function serviceCategory(){
        return $this->hasOne(ServiceCategory::class,'id','service_category_id');
    }
    //店铺总数
    protected function shopNums(): Attribute
    {
        return new Attribute(
            get: fn () => $this->getShop(),
        );
    }
    public function getShop(){
        if($this->attributes['id']>0){
            return ShopDetail::query()
                ->where('customer_id',$this->attributes['id'])
                ->count();
        }else{
            return 0;
        }
    }
    //子账号数
    protected function subAccount(): Attribute
    {
        return new Attribute(
            get: fn () => $this->getAccount(),
        );
    }
    public function getAccount(){
        $shop_arr = $this->getShopArr();
        if(count($shop_arr)>0){
            return SubAccount::query()->whereIn('shop_id',$shop_arr)
                ->count();
        }else{
            return 0;
        }
    }
    //客诉数
    protected function customerComplaints(): Attribute
    {
        return new Attribute(
            get: fn () => $this->customers(),
        );
    }
    protected function customers(){

        $shop_arr = $this->getShopArr();
        if(count($shop_arr)>0){
            return CustomerComplaint::query()->whereIn('shop_id',$shop_arr)
                ->count();
        }else{
            return 0;
        }
    }
//    //客诉数
//    protected function shopKeeper(): Attribute
//    {
//        return new Attribute(
//            get: fn () => $this->shopkeeperquotes(),
//        );
//    }
//    //掌柜语录
//    public function shopkeeperquotes(){
//        $shop_arr = $this->getShopArr();
//        if(count($shop_arr)>0){
//            return ShopkeeperQuote::query()->whereIn('shop_id',$shop_arr)
//                ->count();
//        }else{
//            return 0;
//        }
//    }
    public function getShopArr(){
        return  ShopDetail::query()
            ->where('customer_id',$this->attributes['id'])
            ->pluck('id');
    }
    public function shopDetails(){
        return $this->hasMany(ShopDetail::class,'customer_id');
    }

    public function shop(){
        return $this->hasMany(Shop::class,'customer_id');
    }
}
